Note: This tutorial assumes you have already created your own URDF file or that you are working with the existing PR2 URDF file.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Start using the KDL parser

Description: This tutorial teaches you how to create a KDL Tree from a URDF file

Tutorial Level: BEGINNER

Building the KDL parser

 $ rosdep install kdl_parser

This will install all the external dependencies for the kdl_parser. To build the package, run:

 $ rosmake kdl_parser

Using in your code

First, add the KDL parser as a dependency to your package.xml (in ROS fuerte or earlier it's manifest.xml) file:

  <package>
    ...
    <build_depend package="kdl_parser" />
    ...
    <run_depend package="kdl_parser" />
    ...
  </package>

To start using the KDL parser in your C++ code, include the following file:

   1   #include <kdl_parser/kdl_parser.hpp>
   2 

Now there are different ways to proceed. You can construct a KDL tree from a urdf in various forms:

From a file

  •    1    KDL::Tree my_tree;
       2    if (!kdl_parser::treeFromFile("filename", my_tree)){
       3       ROS_ERROR("Failed to construct kdl tree");
       4       return false;
       5    }
    
    To create the PR2 URDF file, run the following command:
     $ rosrun xacro xacro.py `rospack find pr2_description`/robots/pr2.urdf.xacro > pr2.urdf

From the parameter server

  •    1    KDL::Tree my_tree;
       2    ros::NodeHandle node;
       3    std::string robot_desc_string;
       4    node.param("robot_description", robot_desc_string, std::string());
       5    if (!kdl_parser::treeFromString(robot_desc_string, my_tree)){
       6       ROS_ERROR("Failed to construct kdl tree");
       7       return false;
       8    }
    

From an xml element

  •    1    KDL::Tree my_tree;
       2    TiXmlDocument xml_doc;
       3    xml_doc.Parse(xml_string.c_str());
       4    xml_root = xml_doc.FirstChildElement("robot");
       5    if (!xml_root){
       6       ROS_ERROR("Failed to get robot from xml document");
       7       return false;
       8    }
       9    if (!kdl_parser::treeFromXml(xml_root, my_tree)){
      10       ROS_ERROR("Failed to construct kdl tree");
      11       return false;
      12    }
    

From a URDF model

  •    1    KDL::Tree my_tree;
       2    urdf::Model my_model;
       3    if (!my_model.initXml(....)){
       4       ROS_ERROR("Failed to parse urdf robot model");
       5       return false;
       6    }
       7    if (!kdl_parser::treeFromUrdfModel(my_model, my_tree)){
       8       ROS_ERROR("Failed to construct kdl tree");
       9       return false;
      10    }
    

For more details, take a look at the API documentation.

Wiki: kdl_parser/Tutorials/Start using the KDL parser (last edited 2017-06-02 18:29:06 by DaveKotfis)